const update = (updateHost='') => {
	console.log(updateHost);
	if (updateHost == '') {
		uni.showToast({
			title: '更新方法：更新域名为必传参数',
			icon:'none'
		})
		return;
	}
	// #ifdef APP-PLUS
	let updateUrl = (updateHost.indexOf('com/') >= 0 || updateHost.indexOf('cn/') >= 0) ? updateHost +
		'download/index.php?m=version' : updateHost + '/download/index.php?m=version'
	let deviceType = plus.os.name == 'Android' ? 1 : 2;
	plus.runtime.getProperty(plus.runtime.appid, function(updateInfo) {
		uni.request({
			method: 'POST',
			url: updateUrl,
			header: {
				"Content-Type": "application/x-www-form-urlencoded"
			},
			data: {
				current_version: updateInfo.version,
				client_type: deviceType
			},
			success: (data) => {
				console.log(JSON.stringify(data));
				if (data.data.code == 200) {
					let dask = plus.nativeUI.confirm("系统检测到更新,是否更新", function(e) {
						if (e.index == 0) {
							var showLoading = plus.nativeUI.showWaiting("正在下载");
							var newVersion = data.data.result;
							console.log(JSON.stringify(newVersion))
							if (newVersion.pack_type == 0) {
								console.log(0)
								let dask = plus.downloader.createDownload(newVersion
									.pack_file, {
										filename: "_doc/update/"
									},
									function(d, status) {
										console.log(1)
										if (status == 200) {
											plus.runtime.install(d.filename, {
												force: true
											}, function() {
												plus.nativeUI
													.closeWaiting();
												plus.nativeUI.alert(
													"应用资源更新完成！",
													function() {
														plus.runtime
															.restart();
													});
											}, function(e) {
												plus.nativeUI
													.closeWaiting();
											});
										} else {
											console.log("下载wgt失败！");
										}
										plus.nativeUI.closeWaiting();
									})
								dask.addEventListener("statechanged", function(task) {
									let val = Math.round(task.downloadedSize /
										task.totalSize * 100)
									showLoading.setTitle('努力加载中：' + val + '%')
								});
								dask.start();
							} else {
								console.log(newVersion.pack_file)
								if (deviceType == 1) {
									let dask = plus.downloader.createDownload(newVersion
										.pack_file, {
											filename: "_doc/update/"
										},
										function(d, status) {
											if (status == 200) {
												plus.runtime.install(d.filename, {
													force: true
												}, function() {
													plus.nativeUI
														.closeWaiting();
													plus.nativeUI.alert(
														"应用资源更新完成！",
														function() {
															plus.runtime
																.restart();
														});
												}, function(e) {
													plus.nativeUI
														.closeWaiting();
												});
											} else {
												console.log("下载wgt失败！");
											}
											plus.nativeUI.closeWaiting();
										})
									let current = 0
									dask.addEventListener("statechanged", function(task,
										status) {
										let val = Math.round(task
											.downloadedSize / task
											.totalSize * 100)
										showLoading.setTitle('app更新中：' + val +
											'%')
									});
									dask.start();
								} else {
									plus.runtime.openURL(data.data.pack_url);
								}
							}
						}
					})
				}
			}
		})
	});
	// #endif
}
export default update
